package com.csu.mypetstore.api.controller.front;

import com.alipay.api.AlipayApiException;
import com.csu.mypetstore.api.bean.AliPayBean;
import com.csu.mypetstore.api.common.CommonResponse;
import com.csu.mypetstore.api.entity.LineItem;
import com.csu.mypetstore.api.entity.OrderStatus;
import com.csu.mypetstore.api.entity.Orders;
import com.csu.mypetstore.api.persistence.OrdersMapper;
import com.csu.mypetstore.api.service.OrderService;
import com.csu.mypetstore.api.service.PayService;
import com.csu.mypetstore.api.service.impl.OrderServiceImpl;
import com.csu.mypetstore.api.util.RandomUtils;
import com.csu.mypetstore.api.vo.AccountVO;
import com.csu.mypetstore.api.vo.OrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;

    private final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @RequestMapping("/pay")
    @ResponseBody
    public String alipay(String totalAmount) throws AlipayApiException {
        String outTradeNo = "20220318" + RandomUtils.randomString(5);
        String subject = "mypetstore";
        String body = "宠物订单已支付";
        totalAmount="127.5";

        logger.info("商户订单号为{},订单名称为{},付款金额为{},商品描述{}", outTradeNo, subject, totalAmount, body);
        AliPayBean alipayBean = new AliPayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        alipayBean.setBody(body);

        return payService.aliPay(alipayBean);
    }

    @GetMapping("viewOrders/{username}")
    @ResponseBody
    CommonResponse<List<OrdersVO>> getOrdersByUsername(@PathVariable("username") String username){
        return orderService.getOrdersByUsername(username);
    }

    @GetMapping("orders/{id}")
    @ResponseBody
    CommonResponse<OrdersVO> getOrders(@PathVariable("id")int orderId){
        return orderService.getOrders(orderId);
    }


    @PostMapping("insertOrders")
    @ResponseBody
    CommonResponse<Orders> insertOrders(Orders orders, HttpSession session){

        System.out.println(111);
        CommonResponse<Orders> response = orderService.insertOrders(orders, session);
        OrdersVO ordersVO = (OrdersVO)session.getAttribute("ordersVOSave");
        ordersVO.setOrderId(response.getData().getOrderId());
        ordersVO.setStatus("1");
        session.setAttribute("ordersVOSave", ordersVO);
        return response;

    }


    @PostMapping("updateOrders")
    @ResponseBody
    CommonResponse<Orders> updateOrders(Orders orders){
        return  orderService.updateOrders(orders);
    }

    @PostMapping("updateOrderStatus")
    @ResponseBody
    CommonResponse<OrderStatus> updateOrderStatus(OrderStatus orderStatus){
        return orderService.updateOrderStatus(orderStatus);
    }


    @GetMapping("getLineItemsByOrderId/{id}")
    @ResponseBody
    CommonResponse<List<LineItem>> getLineItemsByOrderId(@PathVariable("id") int orderId){
        return orderService.getLineItemsByOrderId(orderId);
    }

    @PostMapping("saveOrders")
    @ResponseBody
    CommonResponse<OrdersVO> getOrders(OrdersVO ordersVO , HttpSession session){
        CommonResponse<OrdersVO> response = CommonResponse.createForSuccess(ordersVO);
        if(response.isSuccess()){
            session.setAttribute("ordersVOSave", response.getData());
        }
        return response;
    }

    @PostMapping("getInfo")
    @ResponseBody
    CommonResponse<OrdersVO> getInfo(HttpSession session){

        OrdersVO ordersVO = (OrdersVO)session.getAttribute("ordersVOSave");
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        ordersVO.setUsername(accountVO.getUsername());
        CommonResponse<OrdersVO> response = CommonResponse.createForSuccess(ordersVO);
        if(response.isSuccess()){
            session.setAttribute("ordersVOSave", response.getData());
        }
        System.out.println(response.getData());
        return response;
    }


}
